package com.doit.day01;

import org.apache.zookeeper.*;

import java.io.IOException;

public class DataNode {

    ZooKeeper zk = null;

    public static void main(String[] args) throws Exception {
        if (args.length == 1){
            DataNode dataNode = new DataNode();
            dataNode.start(args[0]);
            dataNode.register(args[0]);
            dataNode.service(args[0]);
        }
    }

    //感知上线
    public void start(String hostName) throws IOException {
       zk = new ZooKeeper("linux01:2181", 2000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println(hostName + "上线了");
            }
        });
    }

    /**
     * 当datanode启动之后，需要向zk进行注册
     * 在zk的datanodes节点上创建datanode文件夹
     */
    public  void register(String hostname) throws Exception {
        zk.create("/datanodes/"+hostname,hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
    }


    public  void  service(String hostname) throws Exception {
        System.out.println(hostname+"节点开始工作......");
        Thread.sleep(Integer.MAX_VALUE);
    }









}
